1. Field of the Invention:
The invention relates to the art of numerical processing in a computer system, and rounding during floating point numerical processing in particular.
2. Art Background:
When performing computations in a computer system, it is often necessary to round the results of numeric operations to a certain precision. Rounding occurs routinely in the evaluation of numeric expressions. The rounding mode is used to control the direction and nature of the rounding performed on the results. IEEE Standard 754 specifies four rounding modes which may be employed. They are round to plus infinity, round to minus infinity, round toward zero, and round to nearest value. The round to plus infinity mode causes numbers to be rounded toward positive infinity. The round to minus infinity mode causes numbers to be rounded toward negative infinity. The round toward zero mode causes a number to be rounded toward zero. This is also known as the chop or truncate rounding mode, because it is accomplished by chopping off or truncating the bits of the number to round at the desired precision. The round to nearest (or even) mode rounds the result to the nearest number of the desired precision. When the number is midway between two equally viable results, the round to nearest mode rounds the number to the even result.
The table below shows the results of the various rounding modes on different numbers. The numbers are rounded to the nearest whole number in the table. In general, the rounding may be specified to any precision, not just whole numbers:
TABLE 1 ______________________________________ to 0 nearest +inf -inf (chop) (even) ______________________________________ 1.11 2.0 1.0 1.0 1.0 -1.11 -1.0 -2.0 -1.0 -1.0 1.53 2.0 1.0 1.0 2.0 -1.53 -1.0 -2.0 -1.0 -2.0 2.5 3.0 2.0 2.0 2.0 1.5 2.0 1.0 1.0 2.0 ______________________________________
A common use for rounding modes is during the conversion of floating point numbers to integers. Different programming languages specify different rounding modes in this situation. For example, C language specifies round to zero mode and FORTRAN specifies round to nearest mode as the default mode to use during conversion from the floating point to the integer numeric formats.
Typically, the rounding mode for an operation is specified in one of two ways: static and dynamic. With dynamic rounding control, the rounding mode is specified by the bits of a value in a control register. The control register is typically writeable and readable by software, so that programs can dynamically specify the rounding mode to apply to subsequent numeric operations by writing the control register. The processor interprets the bits of the value in the register and applies the specified rounding mode to all relevant numeric operations. Thus, if the control register was modified to specify the use of round to zero mode, round to zero would be applied to the results of all subsequent numeric operations.
With static rounding control, the rounding mode is coded into a field of the instruction itself. When the processor prepares to execute the instruction, it checks the rounding mode field and applies the specified rounding mode. Thus the rounding mode can be specified on a per-instruction basis without the overhead of writing to a control register.
Some computer systems combine static and dynamic rounding control. Typically, in these systems the instruction contains a field which specifies a certain rounding mode (static rounding control). A control register is also available to specify the rounding mode dynamically. The issue in these systems is which rounding control takes priority, the static or the dynamic.
One solution to the question of priority is to give the static control priority unless the static control specifies a special override value. If the override value is specified, the dynamic rounding control is used. Consider the following encodings for static rounding control:
TABLE 2 ______________________________________ encoding meaning ______________________________________ 000 +inf 001 -inf 010 nearest 011 to 0 (chop) 1xx use mode specified by control register (override) ______________________________________
Some processors, such as certain processors manufactured by the Intel.TM. Corp., use only dynamic rounding control. The use of only dynamic rounding control can lead to software inefficiencies, especially when using library routines that perform mathematical computations. Library routines are typically designed for linking with a variety of programs, so that they are designed to be highly self-contained. Before calling a library routine, a program linked to the library in a system with dynamic rounding control may configure the control register with a rounding mode, then call the library routine to perform the computations. The processor automatically applies the rounding mode specified in the control register to the numeric operations specified in the library routine.
The problem with this approach is that when a library routine relies upon the calling application to specify the rounding mode, it is possible that the application will specify a mode that is undesireable for the routine, violating the library calling convention and jeapordizing the integrity of the calculations performed in the library. To avoid this problem, many library routines will, upon being invoked, first save the current control register value, set the control register to the desired rounding mode, perform their numerical operations, and then restore the saved value of the control register before returning. The overhead associated with first saving the current value of the control register, setting it to a new value, and then restoring the value before returning makes the library routines less efficient than they might otherwise be. However, it ensures the integrity of the calculation and makes it less susceptible to violations of the calling convention.
To make the library routine more efficient, static rounding controls can be used. Using static rounding controls, each instruction specifies a rounding mode which overrides whatever is in the control register. Because of the override, there is no need for the library routine to store and restore the control register value.
The ALPHA.TM. family of microprocessors, manufactured by Digital Equipment.TM. Corp, provide two bits within the instruction for specifying the static rounding control. Two bits allows for the coding of up to four static rounding modes, however, one encoding must be reserved for the dynamic override. The three static modes supported by the ALPHA processors are nearest, round to zero, and round to positive infinity. The ALPHA does not support a static rounding mode for the round to negative infinity mode, because to do so would require a third bit in the encodings (see Table 2). Therefore, library routines which require the round to negative infinity mode must specify dynamic override in their numeric instructions with the control register set to the round to negative infinity mode. Because these routines use the control register to specify the rounding mode, they must incur the overhead associated with saving, setting, and then restoring the control register. The round to negative infinity mode is used for computations which require extra-precise arithmetic, in which a result is rounded to both positive and negative infinity, examined for each case, and the more precise result selected.
To avoid the problems set forth above, it would be desireable to make all four rounding modes available to programs statically, in addition to providing a dynamic override encoding so that the control register value can be used to specify the rounding mode if desired. It would be desireable to accomplish this using only two bits of the instruction for encoding the static rounding mode and dynamic override.